#include "resource.h"

#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "ecutil.h"

#define IDD_EDIT1 1001
#define IDD_TIMER1 1002

HWND win;

BOOL CALLBACK dialogproc(HWND h, UINT m, WPARAM w, LPARAM l)
{
	switch (m) {
	case WM_COMMAND:
		switch (LOWORD(w)) {

			case IDCANCEL:
				SetDlgItemText(h, IDD_EDIT1, L"er...");
				DestroyWindow(h);
				return 1;
			default:
				return 0;
		}

	case WM_TIMER:
		BYTE Hi;
		BYTE Lo;
		long cur;
		WCHAR str[12];

		WmiReadEcRam(0x050a, &Lo);
		WmiReadEcRam(0x050b, &Hi);
		cur = Hi * 0x100 + Lo;
		if (cur > 0xa000)
			cur = cur - 0x10000;
		wsprintf(str, L"%d", cur);
		SetDlgItemText(h, IDD_EDIT1, str);
		break;

	case WM_INITDIALOG:
		win = h;
		InitWMI();
		SetDlgItemText(h, IDD_EDIT1, L"Loading...");
		SetTimer(h, IDD_TIMER1, 1500, NULL);
		break;

	case WM_DESTROY:
		
		CoUninitialize();
		break;
	}
	return 0;
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	CoInitialize(0);
	// main dialog
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, &dialogproc);

	return 0;
}